﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Enterprise.Infrastructure;
using Enterprise.Model.OfficeDocument;
using Enterprise.Service.OfficeDocument;
using Enterprise.Service.Department;
using Enterprise.Service.User;
using Enterprise.Service.WorkFlow;
namespace Enterprise.UI.Web.Modules.OfficeDocument
{
    public partial class Manage : Enterprise.Service.BasePage
    {
        string Id = (string)Utility.sink("Id", Utility.MethodType.Get, 0, 0, Utility.DataType.Str);
        string Cmd = (string)Utility.sink("Cmd", Utility.MethodType.Get, 0, 0, Utility.DataType.Str);
        int typeId = (int)Utility.sink("typeId", Utility.MethodType.Get, 0, 0, Utility.DataType.Int);
        protected string NavString = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                OnStart();
            }
        }

        private void OnStart()
        {
            OnInit();
            OnCommand();
        }

        private void OnInit()
        {
            if (typeId == 0)
            {
                NavString = "起草公文";
                ulTime.Visible = false;
                ulFrom.Visible = false;
                ulJs.Visible = false;
            }
            else if (typeId == 1)
            {
                NavString = "公文流转";
                ul.Visible = false;
                UserService uService = new UserService();
                var list = uService.UserList().Where(p => p.DEPTID == 1).OrderBy(p => p.UORDERBY);
                DeptUser.DataSource = list;
                DeptUser.DataTextField = "UNAME";
                DeptUser.DataValueField = "USERID";
                DeptUser.DataBind();
            }
        }

        private void OnCommand()
        {
            switch (Cmd)
            {
                case "New":
                    int  deptId=0;
                    lb_User.Text = UserService.GetUserName(Utility.Get_UserId,out deptId);
                    lb_Dept.Text = DepartmentService.GetDepartMentName(deptId);
                    tb_FromTime.Text = DateTime.Now.ToString("yyyy年MM月dd日");
                    ulQs.Visible = false;
                    ul1.Visible = false;
                    break;
                case "Revo":
                    OfficeDocService odService = new OfficeDocService();
                    var query = odService.OfficeDocumentDisp(Id);
                    if (query != null&&query.ODUSERID!=0)
                    {
                        query.DB_Option_Action = "Delete";
                        odService.OfficeDocumentExecute(query);
                        //删除流程中待办
                        //同时删除审批流程中的审批节点
                        WorkFlowProcessService wpService = new WorkFlowProcessService();
                        wpService.WorkFlowProcessDelete(Id);

                        Utility.ShowMsg(this.Page, "OK", this.Tran("公文撤销成功！"), "Index.aspx");
                    }
                    break;
                case "Public":
                    OfficeDocService od1Service = new OfficeDocService();
                    var query1 = od1Service.OfficeDocumentDisp(Id);
                    if (query1 != null && query1.ODUSERID != 0)
                    {
                        tb_FromTime.Text = query1.ODFROMTIME;
                        tb_FromUser.Text = query1.ODFROMUSER;
                        tb_From.Text = query1.ODFROM;
                        tb_FromCode.Text = query1.ODFROMCODE;
                        tb_Name.Text = query1.ODCNAME;
                        tb_RName.Text = query1.ODRNAME;
                        t_Content.Text = query1.ODCREMARK;
                        t_ContentRu.Text = query1.ODRREMARK;
                        tb_Files.Text = query1.ODFILES;
                    }
                    ulTime.Visible = false;
                    ulFrom.Visible = false;
                    ulJs.Visible = false;
                    ul.Visible = false;
                    break;
            }
        }


        protected void BtnSave_OnClick(object sender, EventArgs e)
        {
            string _note = "操作成功";
            try
            {
                OfficeDocService oService = new OfficeDocService();
                OfficeDocEntity entity = null;
                if (Cmd == "New")
                {
                    entity = new OfficeDocEntity();
                    entity.DB_Option_Action = "Insert";
                    entity.ODID = Guid.NewGuid().ToString();
                    entity.ODUSERID = Utility.Get_UserId;
                    entity.ODDEPTID = UserService.GetUserDeptId(entity.ODUSERID);
                    entity.ODFROMTIME = (string)Utility.sink(tb_FromTime.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                    entity.ODFROMUSER = (string)Utility.sink(tb_FromUser.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                    entity.ODFROM = (string)Utility.sink(tb_From.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                    entity.ODFROMCODE = (string)Utility.sink(tb_FromCode.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                    entity.ODCNAME = (string)Utility.sink(tb_Name.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                    entity.ODRNAME = (string)Utility.sink(tb_RName.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                    entity.ODCREMARK = (string)Utility.sink(t_Content.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                    entity.ODRREMARK = (string)Utility.sink(t_ContentRu.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                    entity.ODFILES = (string)Utility.sink(tb_Files.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                    entity.ODSTATE = 0;
                    if (typeId == 0)
                    {
                        WorkFlowService wlService = new WorkFlowService();
                        entity.ODCHECKERS = wlService.GetWokrflowCheckUsers(1, entity.ODUSERID, entity.ODDEPTID);
                    }
                    else if (typeId == 1)
                    {
                        int u = (int)Utility.sink(DeptUser.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Int);
                        entity.ODCHECKERS = u.ToString();
                    }
                    entity.ODTYPEID = typeId;
                    entity.ODCREATETIME = DateTime.Now;
                    entity.ODQSUSERS = "";
                    entity.ODISPUBLIC = 0;
                    entity.ODPUBLICTIME = DateTime.Now;       
                }
                else if (Cmd == "Public")
                {
                    entity = oService.OfficeDocumentDisp(Id);
                    entity.DB_Option_Action = "Public";
                    entity.ODTYPE = (string)Utility.sink(tb_Type.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                    entity.ODCODE = (string)Utility.sink(tb_Code.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                    entity.ODCNAME = (string)Utility.sink(tb_Name.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                    entity.ODRNAME = (string)Utility.sink(tb_RName.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                    entity.ODCREMARK = (string)Utility.sink(t_Content.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                    entity.ODRREMARK = (string)Utility.sink(t_ContentRu.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                    entity.ODFILES = (string)Utility.sink(tb_Files.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                    //签收范围格式@1,4,5
                    string rcv_users = rcv_users_value.Value;
                    rcv_users = rcv_users.TrimEnd(',');
                    entity.ODQSUSERS = rcv_users;//签收范围   
                    entity.ODISPUBLIC = 1;
                    entity.ODPUBLICTIME = DateTime.Now;       
                }
                oService.OfficeDocumentExecute(entity);
                if (Cmd == "New")
                {
                    //发送待办消息至审批人处进行相关审批
                    oService.Notice(entity, 0);
                }
                if (Cmd == "Public")
                {
                    Enterprise.Service.Rtx.RtxService.SendRtxMessageServices(entity.ODQSUSERS, "办公事务 - 公文挂历", "发布新公文：" + entity.ODCNAME, false);
                }
            }
            catch (Exception ex)
            {
                _note = ex.Message;
            }
            Utility.ShowMsg(this.Page, "OK", this.Tran(_note), "Index.aspx");
        }

    }
}